package readwrite;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Connector
{
    /**
     * Necessary variables
     */
    private String url;                 //url to DB
    private String username;            //DB username
    private String password;            //DB password
    private Connection connecton;       //variable for connection
    private String query;               //DB query
    private PreparedStatement statment; //create query
    private ResultSet result;           //variable to execute query
    private boolean userExist;          //check at user is in the DB
    /**
     * Constructor
     */
    public Connector() throws Exception
    {
        //obtain the necessary information from the configuration file
        Config config = new Config();
        url = config.getUrl();
        username = config.getUserName();
        password = config.getPassword();
        //Accessing driver
        Class.forName("com.mysql.jdbc.Driver");
        //establish a database connection
        connecton = DriverManager.getConnection(url, username, password);
    }
    
    /**
     * 
     * @param _username - username
     * @param _password - password
     * @return true if username and password correct, and false if they don't correct
     * @throws Exception 
     */
    public boolean LoginStatement(String _username, String _password) throws Exception
    {   
        //Set that we dont user
        userExist = false;
        //query whick returns list with users from User table
        query = "select UserName from User";
        //Send our query to statment
        statment = connecton.prepareStatement(query);
        //Execute query
        result = statment.executeQuery();
        
        //Check all users from the DB
        while(result.next())
        {
            if(result.getString("UserName").equals(_username))
            {
                userExist = true;
                //System.out.println(result.getString("UserName"));
                break;
            }
        }
        
        //If we found user in the DB we should check user's password
        if (userExist = true)
        {
            //query which select password for specified user
            query = "select Password from User where UserName = " + "'" +_username + "'";
            //System.out.println(query);
            statment = connecton.prepareStatement(query);
            result = statment.executeQuery();
            result.next();
            
            //Compair the specified password with the saved one
            if(result.getString("Password").equals(_password))
            {  
                System.out.println("Password correct:");
                System.out.println("Username - " + _username);
                System.out.println("Password - " + result.getString("Password"));        
                userExist = false;
                return true;
            }
            else
            {
                System.out.println("Password incorrect");
                userExist = false;
                return false;
            }
        }
        //User with specified name doesn't exist in the DB
        else
        {
            userExist = false;
            return false;
        }   
        
    }
    
}//end of Connector
